package ProjectTeam39.Networking;

import java.io.IOException;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
	
	private Model model;
	private int port;
	private ExecutorService pool;
	private static final int MAX_CONNECTIONS = 5;
	
	public Server(int port){
		this.port = port;
		pool = Executors.newFixedThreadPool(MAX_CONNECTIONS);
	}
	
	public void run(){
		ServerSocket theServer;
		Socket theClient = null;
		
		try{
			System.out.println("Starting server");
			theServer = new ServerSocket(port);
			while(true){
				theClient = theServer.accept();
				MessageHandler mh = new MessageHandler(theClient, model);
				pool.execute(mh);
			}
		}
		catch(IOException e){
			System.err.println("Error in Server Socket accept");
            e.printStackTrace();
		}
	}
}
